package com.chen.controller;

import com.chen.annotation.WxLoginUser;
import com.chen.bean.result.Result;
import com.chen.bean.vo.DialogVo;
import com.chen.bean.vo.MessageRequest;
import com.chen.bean.vo.MessageVo;
import com.chen.exception.BusinessException;
import com.chen.service.ChatService;
import com.chen.service.DialogService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/wx")
public class ChatController {

    @Autowired
    private ChatService chatService;

    @Autowired
    private DialogService dialogService;

    /**
     * 对话接口
     * @param request
     * @return
     * @throws BusinessException
     */
    @PostMapping(value = "/chat",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chat(@RequestBody @Valid MessageRequest request,@WxLoginUser Integer userId) throws BusinessException {
        return chatService.chat(request,userId);
    }

    /**
     * 获取历史消息
     */
    @GetMapping("/historyDialogs")
    public Result<List<DialogVo>> queryHistoryDialogs(@WxLoginUser Integer userId){
        return Result.success(dialogService.queryHistoryDialogs(userId));
    }

    /**
     * 根据dialogNumber查询对话详情
     */
    @GetMapping("/dialogDetail")
    public Result<List<MessageVo>> queryDialogDetail(@WxLoginUser Integer userId,@RequestParam @Valid @NotBlank(message = "对话编号不能为空") String dialogNumber){
        return Result.success(dialogService.queryByDialogNumber(dialogNumber,userId));
    }

    /**
     * 获取对话编号
     */
    @GetMapping("/generatorDialogNumber")
    public Result<String> generatorDialogNumber(@WxLoginUser Integer userId){
        return Result.success(chatService.generatorDialogNumber(userId));
    }
}
